// pages/home/home.js
const db = wx.cloud.database()
var app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        hasUserInfo: false,
        rewWordsNum: 0,
        oldWordsNum: 0,
        unstudyWordsNum: 0,
        multiArray: [
            ['自动播放', '滑动学习'],
            [true, false]
        ],
        multiIndex: [0, 0],
        arrColumn0: [true, false],
        arrColumn1: [true, false]
    },

    toReview: function () {
        wx.navigateTo({
            url: '/pages/study/reviewWord/reviewWord',
        })
    },

    toTopList(){
      wx.navigateTo({
        url: '/pages/audio/audioTobList/audioTobList',
      })
    },

    status: function () {
        wx.navigateTo({
            url: '/pages/my/statistics/statistics',
        })
    },

    weixin() {
        this.component.show()
    },

    setClipboardData() { // 复制微信号
        wx.setClipboardData({
            data: "hw704530",
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data) // data
                    }
                })
            }
        })
    },

    onReady: function () {
        // 绑定组件id，用于后面调用指定id组件内的方法
        this.component = this.selectComponent("#weixin")
    },

    wordList(res) {
        var id = res.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/wordBook/wordBookList/wordBookList?id=" + id,
        })
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // db.collection("wordlist").count().then(res => {
        //     // console.log('res',res.total)
        //     this.setData({
        //         count: res.total || 0
        //     })
        //     // console.log('res.total',res.total)
        //     wx.setStorageSync('notebook', res.total)
        // })
        let {
            rewWordsNum
        } = this.data
        if (wx.getStorageSync('cet6Progress') && wx.getStorageSync('cet4Progress')) {
            rewWordsNum = wx.getStorageSync('cet6Progress') + wx.getStorageSync('cet4Progress')
            this.setData({
                rewWordsNum: rewWordsNum || 0
            })
        } else {
            rewWordsNum: 0
        }
        console.log('rew_cnt', rewWordsNum)
    },
    /**
     * 用户注销
     */
    onClickOut: function () {
        app.delUserInfo();
        this.setData({
            userInfo: null
        })
    },

    // 点击登录授权获取头像、昵称
    async getUserProfile(e) {
        await wx.getUserProfile({
            desc: '用于完善会员资料',
            success: (e) => { // 授权成功调用云函数获取OPENDID
                console.log(e.userInfo)
                wx.showLoading({
                    title: "正在加载数据",
                    mask: true
                })
                this.setData({
                    userInfo: e.userInfo, // 展示头像、昵称
                    hasUserInfo: true
                })
                wx.cloud.callFunction({ // 用户进入缓存已经存入OPNEID
                    name: 'getUserOpenid',
                }).then(res => {
                    let that = this
                    let OPENID = res.result.OPENID
                    // console.log('云函数', OPENID) //res就将appid和openid返回了
                    wx.cloud.callFunction({
                        name: "getDictWord",
                        data: {
                            type: 'isNewUser',
                            openid: OPENID
                        }
                    }).then(res => {
                        console.log('res', res)
                        let {
                            result
                        } = res
                        
                        console.log('result',result)
                        if (!result || !result[0]) { // 判断是否为新用户
                            console.log('判断新用户', result.data,OPENID)
                            wx.cloud.callFunction({
                                name: 'addDictWord',
                                data: {
                                    type: 'newUser',
                                    openid: OPENID,
                                    userName: e.userInfo.nickName
                                }
                            }).then(() => {
                                wx.hideLoading()
                            })
                        } else {
                            console.log('老用户')
                            new Promise(() => {
                                if (!wx.getStorageSync('cet4Progress') && !wx.getStorageSync('cet6Progress') && !wx.getStorageSync('lineData')) {

                                    wx.cloud.callFunction({
                                        name: "getDictWord",
                                        data: {
                                            type: "isOldUser",
                                            openid: OPENID
                                        }
                                    }).then(res => {
                                        console.log(res)
                                        let result = res.result.data
                                        if (result && result[0]){
                                            wx.setStorageSync('cet4Progress', result[0].cet4Progress)
                                            wx.setStorageSync('cet6Progress', result[0].cet6Progress)
                                            wx.setStorageSync('kaoyanProgress', result[0].kaoyanProgress)
                                            wx.setStorageSync('lineData', result[0].lineData)
                                        }
                                        wx.hideLoading({})
                                    })

                                }else{
                                    wx.hideLoading({})
                                }
                               
                            })

                        }
                    })
                    wx.setStorageSync('userInfo', {
                        openid: OPENID,
                        userInfo: {
                            avatarUrl: that.data.userInfo.avatarUrl,
                            nickName: that.data.userInfo.nickName
                        }
                    })
                })
            }
        })
    },

    // 更新用户信息
    async updataUserProfile() {
        await wx.getUserProfile({
            desc: '用于完善会员资料',
            success: (res) => {
                console.log('upinfo', res.userInfo)
                if (wx.getStorageSync('userInfo')) {
                    let infor = wx.getStorageSync('userInfo')
                    wx.setStorageSync('userInfo', {
                        openid: infor.openid,
                        userInfo: {
                            avatarUrl: res.userInfo.avatarUrl,
                            nickName: res.userInfo.nickName
                        }
                    })
                }
                this.setData({
                    userInfo: res.userInfo, // 展示头像、昵称
                    hasUserInfo: true
                })
            }
        })
    },

    // 我的页面
    async onLoad(options) {
        let userInfo = await wx.getStorageSync('userInfo')
        console.log('登录模块的OPENID', userInfo.openid)
        if (userInfo) {
            this.setData({
                userInfo: userInfo.userInfo,
                hasUserInfo: true
            })
        } else { // 没有缓存用户昵称和头像就弹出登录请求
            this.setData({
                hasUserInfo: false
            })
        }
    },
    // 跳转已掌握页面
    over: function () {
        wx.navigateTo({
            url: '/pages/wordBook/masterWordList/masterWordList',
        })
    },

    // 跳转收藏页面
    star() {
        wx.navigateTo({
            url: '/pages/wordBook/starWordList/starWordList',
        })
    },

    // 多列选择器
    PickerChange(e) {
        let {
            multiArray
        } = this.data
        let value = e.detail.value
        this.setData({
            multiIndex: value
        })
        console.log(this.data.multiArray[0][value[0]], this.data.multiArray[1][value[1]])
        if (multiArray[0][value[0]] == '滑动学习') {
            multiArray[1][value[1]] == true ? wx.setStorageSync('swiperLear', true) : wx.setStorageSync('swiperLear', false)
        } else if (multiArray[0][value[0]] == '自动播放') {
            multiArray[1][value[1]] == true ? wx.setStorageSync('autoplay', true) : wx.setStorageSync('autoplay', false)
        }

    },
    PickerColumnChange(e) {
        // 先定义数据，数组里面两个数组，意为两列，当第一列发生改变时侯，给数组重新赋值

        let obj = e.detail,
            multiArray = this.data.multiArray;
        if (obj.column == 0 && obj.value == 0) {
            multiArray[1] = this.data.arrColumn0
        }
        if (obj.column == 0 && obj.value == 1) {
            multiArray[1] = this.data.arrColumn1
        }
        this.setData({
            multiArray: multiArray
        })
    },
})